# Copyright 2018-2024 Intel Corporation
# Copyright 2025 Google LLC
# Copyright 2025 Hewlett Packard Enterprise Development LP
# All rights reserved.
#
# 'recipe' for Docker to build an image of EL 8 based
# environment for building the DAOS project.
#

# Pull base image
ARG POINT_RELEASE=
ARG BASE_DISTRO=rockylinux/rockylinux:8$POINT_RELEASE
FROM $BASE_DISTRO AS basic
LABEL maintainer="daos@daos.groups.io"
# Needed for later use of BASE_DISTRO
ARG BASE_DISTRO

# Intermittent cache-bust.  Used to reduce load on the actual CB1 later.
ARG CB0

ARG REPO_FILE_URL
ARG JENKINS_URL
ARG REPOS
ARG DAOS_LAB_CA_FILE_URL
# script to install OS updates basic tools and daos dependencies
COPY ./utils/scripts/install-el8.sh /tmp/install.sh
# script to setup local repo if available
COPY ./utils/scripts/helpers/repo-helper-el8.sh /tmp/repo-helper.sh

RUN chmod +x /tmp/repo-helper.sh /tmp/install.sh && \
    /tmp/repo-helper.sh &&                          \
    rm -f /tmp/repo-helper.sh

FROM basic
# Install OS updates and package.  Include basic tools and daos dependencies
# The proxy variables are currently needed for the combination of running
# with a local repository, yet needing a proxy to reach outside repositories.
# This needs to be moved to a shell script like above in the future to
# properly only remove the proxy variables only when they need to be removed
RUN if [ -n "$REPO_FILE_URL" ]; then direct="${REPO_FILE_URL##*//}; "\
        direct="${direct%%/*}"; export no_proxy="${direct}"; fi; \
    dnf upgrade &&        \
    /tmp/install.sh &&    \
    dnf clean all &&      \
    rm -f /tmp/install.sh

# Add DAOS users
ARG UID=1000
COPY ./utils/scripts/helpers/daos-server-user-setup.sh \
     /tmp/daos-server-user-setup.sh
RUN set -e;                                    \
    chmod +x /tmp/daos-server-user-setup.sh && \
    /tmp/daos-server-user-setup.sh
RUN useradd --no-log-init --user-group --create-home --shell /bin/bash daos_agent
RUN echo "daos_agent:daos_agent" | chpasswd

# Create directory for DAOS backend storage
RUN mkdir -p /opt/daos /mnt/daos /var/run/daos_server /var/run/daos_agent /home/daos/pre /home/daos/daos &&   \
    chown -R daos_server.daos_server /opt/daos /mnt/daos /var/run/daos_server /home/daos &&  \
    chown daos_agent.daos_agent /var/run/daos_agent

ARG JENKINS_URL

USER daos_server:daos_server

# Setup a python venv so that python packages can be installed locally.
RUN python3 -m venv /home/daos/venv
ENV PATH=/home/daos/venv/bin:$PATH
ENV VIRTUAL_ENV=/home/daos/venv/

# Install latest versions of python tools.
COPY requirements-build.txt requirements-utest.txt ./
RUN python3 -m pip --no-cache-dir install --upgrade pip && \
    python3 -m pip --no-cache-dir install -r requirements-build.txt -r requirements-utest.txt

WORKDIR /home/daos/pre
RUN mkdir -p /home/daos/pre/site_scons/prereq_tools /home/daos/pre/site_scons/components
COPY --chown=daos_server:daos_server SConstruct .
COPY --chown=daos_server:daos_server deps deps
COPY --chown=daos_server:daos_server site_scons/prereq_tools site_scons/prereq_tools
COPY --chown=daos_server:daos_server site_scons/components site_scons/components
COPY --chown=daos_server:daos_server utils/build.config utils/
COPY --chown=daos_server:daos_server utils/scripts/copy_files.sh utils/scripts/copy_files.sh
COPY --chown=daos_server:daos_server utils/scripts/create_spdk_pkgconfig.sh utils/scripts/create_spdk_pkgconfig.sh

# Control what to build.  By default Dockerfiles build everything to allow for
# ease-of-use for users, however in CI everything is turned off and then
# selectively enabled.  Turning off any step disables all future steps.
ARG DAOS_DEPS_BUILD=yes
ARG DAOS_KEEP_BUILD=no
ARG DAOS_TARGET_TYPE=release
ARG DAOS_PACKAGES_BUILD=yes

# Now do an update to ensure software is up to date for the deps build.  If the
# src hasn't changed then this won't do anything, but if it has then we want to
# ensure that latest dependencies are used.
USER root:root
RUN [ "$DAOS_DEPS_BUILD" != "yes" ] || {                                               \
        if [ -n "$REPO_FILE_URL" ]; then direct="${REPO_FILE_URL##*//}; "              \
            direct="${direct%%/*}"; export no_proxy="${direct}"; fi;                   \
        dnf upgrade --exclude=spdk,spdk-devel,dpdk-devel,dpdk,mercury-devel,mercury && \
        dnf clean all;                                                                 \
    }
USER daos_server:daos_server

ARG DEPS_JOBS=1

RUN [ "$DAOS_DEPS_BUILD" != "yes" ] || {                            \
        if [ -n "$REPO_FILE_URL" ]; then direct="${REPO_FILE_URL##*//}; "\
            direct="${direct%%/*}"; export no_proxy="${direct}"; fi; \
        scons --build-deps=only --jobs $DEPS_JOBS PREFIX=/opt/daos  \
              TARGET_TYPE=$DAOS_TARGET_TYPE &&                      \
        ([ "$DAOS_KEEP_BUILD" != "no" ] || /bin/rm -rf build *.gz); \
    }

COPY --chown=daos_server:daos_server utils/rpms utils/rpms
COPY --chown=daos_server:daos_server utils/sl utils/sl

# Build third party RPMs
RUN [ "$DAOS_PACKAGES_BUILD" != "yes" ] || [ "$DAOS_DEPS_BUILD" != "yes" ] || { \
        if [ -n "$REPO_FILE_URL" ]; then direct="${REPO_FILE_URL##*//}; "\
            direct="${direct%%/*}"; export no_proxy="${direct}"; fi; \
	utils/rpms/build_packages.sh deps &&                                    \
	mkdir -p /home/daos/rpms &&                                             \
	mv *.rpm /home/daos/rpms;                                               \
    }
USER root:root

# force an upgrade to get any newly built RPMs, but only if CB1 is set.
ARG CB1
RUN [ -z "$CB1" ] || {                                                     \
    if [ -n "$REPO_FILE_URL" ]; then direct="${REPO_FILE_URL##*//}; "      \
        direct="${direct%%/*}"; export no_proxy="${direct}"; fi;           \
    dnf upgrade                                                            \
        --exclude=spdk,spdk-devel,dpdk-devel,dpdk,mercury-devel,mercury && \
    dnf clean all;                                                         \
    }
USER daos_server:daos_server

# Set a label.  This is useful for searching for DAOS images, but is also used
# in github-actions to prune elements of the dockerfile below this point.
LABEL DAOS=true

WORKDIR /home/daos/daos/
COPY --chown=daos_server:daos_server VERSION LICENSE ftest.sh SConstruct requirements-ftest.txt .clang-format ./
COPY --chown=daos_server:daos_server site_scons site_scons
COPY --chown=daos_server:daos_server src src
COPY --chown=daos_server:daos_server utils/build.config utils/
COPY --chown=daos_server:daos_server utils/config utils/config
COPY --chown=daos_server:daos_server utils/certs utils/certs
COPY --chown=daos_server:daos_server utils/completion utils/completion

# select compiler to use
ARG COMPILER=gcc
ARG JOBS=$DEPS_JOBS
ARG DAOS_BUILD_TYPE=$DAOS_TARGET_TYPE
ARG DAOS_BUILD=$DAOS_DEPS_BUILD

# Build DAOS
RUN [ "$DAOS_BUILD" != "yes" ] || {                                        \
        if [ -n "$REPO_FILE_URL" ]; then direct="${REPO_FILE_URL##*//}; "  \
            direct="${direct%%/*}"; export no_proxy="${direct}"; fi;      \
        scons --jobs $JOBS install PREFIX=/opt/daos COMPILER=$COMPILER     \
              FIRMWARE_MGMT=1 BUILD_TYPE=$DAOS_BUILD_TYPE TARGET_TYPE=$DAOS_TARGET_TYPE && \
        ([ "$DAOS_KEEP_BUILD" != "no" ] || /bin/rm -rf build) &&           \
        go clean -cache &&                                                 \
        cp -r utils/config/examples /opt/daos;                             \
    }

COPY --chown=daos_server:daos_server utils utils

# Build DAOS RPMs
RUN [ "$DAOS_PACKAGES_BUILD" != "yes" ] || [ "$DAOS_BUILD" != "yes" ] || { \
        if [ -n "$REPO_FILE_URL" ]; then direct="${REPO_FILE_URL##*//}; "  \
            direct="${direct%%/*}"; export no_proxy="${direct}"; fi;       \
	utils/rpms/build_packages.sh daos &&                               \
	mkdir -p /home/daos/rpms &&                                        \
	cp *.rpm /home/daos/rpms;                                          \
    }

# Set environment variables
ENV PATH=/opt/daos/bin:$PATH
ENV FI_SOCKETS_MAX_CONN_RETRY=1

# Build java and hadoop bindings
WORKDIR /home/daos/daos/src/client/java

ARG DAOS_JAVA_BUILD=$DAOS_BUILD

RUN [ "$DAOS_JAVA_BUILD" != "yes" ] || {                                                      \
        if [ -n "$REPO_FILE_URL" ]; then direct="${REPO_FILE_URL##*//}; "                     \
            direct="${direct%%/*}"; export no_proxy="${direct}"; fi;                          \
        mkdir /home/daos/.m2 &&                                                               \
        cp /home/daos/daos/utils/scripts/helpers/maven-settings.xml.in /home/daos/.m2/settings.xml &&      \
        export JAVA_HOME=$(daos-java/find_java_home.sh) && \
        mvn clean install -T 1C                                                               \
            -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
            -DskipITs -Dgpg.skip -Ddaos.install.path=/opt/daos;                               \
    }
WORKDIR /home/daos

ARG DAOS_KEEP_SRC=no
# Remove local copy
RUN [ "$DAOS_KEEP_SRC" != "no" ] || rm -rf /home/daos/daos /home/daos/pre
